package com.springboot.autoconfigure.oss.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.springboot.autoconfigure.oss.config.OssConfigPropertis;
import com.springboot.autoconfigure.oss.service.FileNameService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;


/**
 * 默认文件名生成策略
 */
public class DefaultFileNameServiceImpl implements FileNameService {

    @Override
    public String getFileName(File file) {
        OssConfigPropertis bean = SpringUtil.getBean(OssConfigPropertis.class);
        if (bean.isNativeFileName())return file.getName();
        StringBuilder builder = new StringBuilder();
        if (StringUtils.isNoneBlank(bean.getFilePrefix())){
            builder.append(bean.getFilePrefix()); //  文件前缀
        }
        builder.append(UUID.fastUUID().toString().replace("-","")); //  uuid
        builder.append(bean.getFilePathSplit());// 文件路径分隔符
        builder.append(DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMddHHmmssSSS"))); // 时间
        builder.append(StrUtil.DOT);
        builder.append(FileUtil.extName(file.getName()));
        return builder.toString();
    }

    @Override
    public String getFilePath() {
        String date = DateUtil.format(new Date(), FastDateFormat.getInstance("yyyy/MM/"));
        return  date.replace("/",File.separator);
    }
}
